#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;
use lib ('/home/chef/projects/mag/ldap-schema-generator');
use Ldif::Parser;
use Ldif::Creator;
use JSON;
use Net::LDAP::LDIF;
use Net::LDAP::Entry;


my $parser = Ldif::Parser->new;
$parser->parse_file('/home/chef/projects/mag/openldap/schema/core.ldif');
$parser->parse_file('/home/chef/projects/mag/openldap/schema/openldap.ldif');
my $classes = $parser->get_olc_object_classes;


my @arr ;
push @arr, {dn=>'aaa',cn=>'aaaaaaa', qq=>'sdf', qq1=> 'asdfds'};
push @arr, {dn=>'bb.aaa',ou=>'bbaa'};
push @arr, {dn=>'cc.aaa',gid=>'ccaaassssdsf sdf sdf sdf sdf dsf sf',objectClass => "top posixAccount"};
$a= to_json(\@arr);
print Dumper ($a, from_json($a));

my $creator = Ldif::Creator->new;
$creator->create_ldif($a,"/home/chef/projects/mag/openldap/qq1.ldif");

print Dumper ($creator->read_from_ldif("/home/chef/projects/mag/openldap/qq1.ldif"));
# print Dumper $classes;

# print "\n\n".Dumper($parser->get_object_class_by_name('organizationalPerson'));
# print Dumper($parser->get_object_class_by_name('person'));

# $parser->clear_object_classes;

# print Dumper $parser->get_object_classes_names ;

# $parser->parse_object_classes;
# print Dumper $parser->get_parsed_classes;

# print "\n\n".Dumper($parser->get_parsed_object_class_by_name('organizationalPerson'));
# print Dumper($parser->get_parsed_object_class_by_name('person'));



# my $ldif = Net::LDAP::LDIF->new( "/home/chef/projects/mag/openldap/qq.ldif", "r", onerror => 'undef' );

# my $ldif_new = Net::LDAP::LDIF->new( "/home/chef/projects/mag/openldap/qq1.ldif", "w", onerror => 'undef', sort => 1 );
# my $entry;
# while ( not $ldif->eof() ) {
# 	$entry = $ldif->read_entry();
# 	if ( $ldif->error() ) {
# 		print "Error msg: ",    $ldif->error(),       "\n";
# 		print "Error lines:\n", $ldif->error_lines(), "\n";
# 	}
# 	else {
# 		$entry->add('seeAlso'=> 'qwerty');
# 		$ldif_new->write_entry($entry);
# 	}
# }
# $ldif_new->write_entry($entry);
# $ldif->done();
# $ldif_new->done();

# my $schema = "

# olcAttributeTypes: ( 2.5.4.46 NAME ( 'dnQualifier' 'qwqe' )
#   DESC 'RFC2256: DN qualifier'
#   EQUALITY caseIgnoreMatch
#   ORDERING caseIgnoreOrderingMatch
#   SUBSTR caseIgnoreSubstringsMatch
#   SYNTAX 1.3.6.1.4.1.1466.115.121.1.44
#   SINGLE-VALUE

#   )

# ";

# my (@parsed_arr) = $schema =~ m/
#     olcAttributeTypes: \s*
#       \(
#                  \s*         (.*?)    \s+
#         (?: NAME \s+  \(? \s* (.*?)    \s* \)?   )?
#         (?: DESC \s+       ' (.*?) '  \s+ )?
#         (?: OBSOLETE   \s+ )?
#         (?: SUP  \s+         (.*?)    \s+ (.*?) \s+ )?
#         (?: EQUALITY \s* \(? \s+ (.*?)    \s* \)?    \s+ )?
#         (?: ORDERING  \s* \(? \s+ (.*?)    \s* \)?    \s+ )?
#         (?: SUBSTR \s+        (.*?)   \s+ )?
#         (?: SYNTAX \s+        (.*?)   \s+ )?
#         (?: (SINGLE-VALUE) \s+ )?
#         (?: (COLLECTIVE) \s+ )?
#         (?: (NO-USER-MODIFICATION) \s+ )?
#         (?: USAGE  \s+ (.*?) \s* )?
#       \)
#   /sgx;

# my $olcAttributeTypes;

# if ( defined $parsed_arr[1] ) {
#     ( my $names = $parsed_arr[1] ) =~ s/\'/ /g;
#     my @names = split( ' ', $names );

#     $olcAttributeTypes->{names}->{$_} = '' foreach @names;
# }

# print Dumper $olcAttributeTypes;
